﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using VehicleConfigurator.Model;
using VehicleConfigurator.Database;
using VehicleConfigurator.NetUtils.Database;
using VehicleConfigurator.NetUtils;
using log4net;

namespace VehicleConfigurator
{
    public partial class frmVat : Form
    {
        public frmVat()
        {
            InitializeComponent();
        }
        // user function
        void ReFormatGrid()
        {
            dgvVat.AllowUserToAddRows = false;
            dgvVat.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
            dgvVat.ScrollBars = ScrollBars.Both;
        }
        void LoadData()
        {
            DataTable dt = new DataTable();
            dt = DBAccess.GetVat();
            this.dgvVat.DataSource = dt;
        }
        Vat RowToVat(DataRow r)
        {
            Vat v = new Vat();
            v.OID = (int)r[FieldNames.OID];
            v.VatName = r[FieldNames.VAT_NAME] == null ? "" : r[FieldNames.VAT_NAME].ToString();
            v.Code = r[FieldNames.VAT_CODE] == null ? "" : r[FieldNames.VAT_CODE].ToString();
            v.VATPC = r[FieldNames.VAT_VATPC] == DBNull.Value ? "0" : r[FieldNames.VAT_VATPC].ToString();//double.Parse(r[FieldNames.VAT_VATPC].ToString());
            return v;

        }

        void SaveGrid(DataTable dt)
        {
            //add new
            var added = dt.GetChanges(DataRowState.Added);
            if (added != null && added.Rows.Count > 0)
            {
                List<Vat> la = new List<Vat>();
                foreach (var item in added.Rows)
                {
                    var r = item as DataRow;
                    var v = RowToVat(r);
                    la.Add(v);
                }
                DBAccess.CreateBulkVat(la);
            }
            //update
            var updated = dt.GetChanges(DataRowState.Modified);
            if (updated != null && updated.Rows.Count > 0)
            {
                //List<Vat> la = new List<Vat>();
                foreach (var item in updated.Rows)
                {
                    var r = item as DataRow;
                    var v = RowToVat(r);
                    // la.Add(v);
                    DBAccess.UpdateVat(v);
                }

            }
            //delete
            var deleted = dt.GetChanges(DataRowState.Deleted);
            var ids1 = Utils.getIdDelete(deleted, FieldNames.OID);
            DBAccess.DeleteByOid(FieldNames.Table.VAT, ids1);
            dt.AcceptChanges();


        }




        // control event

        private void frmVat_Load(object sender, EventArgs e)
        {
            try
            {
                this.LoadData();
                this.ReFormatGrid();
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message);
            }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnNew_Click(object sender, EventArgs e)
        {
            DataTable dt = new DataTable();
            dt = (DataTable)dgvVat.DataSource;
            dt.Rows.Add(-1, "VAT", "Name", 10.2);
            dgvVat.DataSource = dt;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {

            try
            {
                this.SaveGrid(this.dgvVat.DataSource as DataTable);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            DataTable dt = (DataTable)this.dgvVat.DataSource;
            foreach (DataGridViewRow item in this.dgvVat.SelectedRows)
            {
                dgvVat.Rows.RemoveAt(item.Index);
            }
        }

        private void dgvVat_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
        {
            if (dgvVat.Columns[e.ColumnIndex].Name == FieldNames.VAT_VATPC)
            {
                //MessageBox.Show(e.FormattedValue.ToString());
                double a;
                if (!double.TryParse(e.FormattedValue.ToString(), out a))
                {
                    e.Cancel = true;
                    dgvVat.Rows[e.RowIndex].ErrorText = "VATPC must be numeric !";
                   // e.Cancel = true;

                }
            }
            // Must be numeric


        }

        private void dgvVat_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            dgvVat.Rows[e.RowIndex].ErrorText = string.Empty;
        }

    }
}
